﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class album : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int albumId = Convert.ToInt32(Request.Params["albumId"]);
                BLL.AlbumBLL albumBLL = (BLL.AlbumBLL)BLLFAC.DataAccess.CreateAlbum();
                DAL.AlbumDAL albumDAL = (DAL.AlbumDAL)DALFAC.DataAccess.CreateAlbum();
                DAL.PhotoDAL photoDAL = (DAL.PhotoDAL)DALFAC.DataAccess.CreatePhoto();
                Model.Album albumModel = albumDAL.GetModel(albumId);
                Label4.Text = albumModel.getAB_albumName();
                List<Model.Photo> photolist = photoDAL.getListByAlbum(albumId);

                if (0 != photolist.Capacity)
                {
                    Repeater1.DataSource = photolist;
                    Repeater1.DataBind();
                }
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            string userKey = TextBox_Search.Text;
            string url = "SearchResult.aspx?key=" + TextBox_Search.Text;
            Response.Redirect(url);
        }

        protected void Quit_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Index.aspx");
        }

        protected void Upload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                bool fileOK = false;
                string path = "~/Img";


                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                string name = FileUpload1.FileName;
                string savepath = Server.MapPath(path + "\\" + name);
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;

                    }

                    if (fileOK)
                    {
                        FileUpload1.SaveAs(savepath);
                        BLL.PhotoBLL photoBll = (BLL.PhotoBLL)BLLFAC.DataAccess.CreatePhoto();
                        int albumId = Convert.ToInt32(Request.Params["albumId"]);
                        Model.Photo photo = new Model.Photo();
                        photo.setPT_albumId(albumId);
                        photo.setPT_photoName(name);
                        photo.setPT_generateTime(DateTime.Now);
                        photo.setPT_impression(TextBox_Imp.Text);
                        photo.setPT_commentCount(0);
                        photo.setPT_photoRef("../Img/" + name);

                        if (null != photoBll)
                        {
                            if (photoBll.uploadPhoto(photo))
                            {
                                DAL.AlbumDAL albumDal = (DAL.AlbumDAL)DALFAC.DataAccess.CreateAlbum();
                                Model.Album albumModel = albumDal.GetModel(albumId);
                                int photoCount = albumModel.getAB_photoCount() + 1;
                                albumModel.setAB_photoCount(photoCount);
                                albumDal.Update(albumModel);
                            }
                            else
                            {
                            }
                        }
                    }

                }


            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

        }
    }
}